<?php

$foo=$_GET["projectid"];
$all=$_GET["all"];

if (!$all) {$limit="limit 10";}
else {$limit="";}

// draws table headers
function drawtableheader()
{
echo"<div id=tiny>Showing 10 most recent updates. <a href='editproj.php?projectid=" . $_GET["projectid"] . "&all=1'>Show everything</a> </div>
<table width=85%>
<tr><th width=9%>Last Updated</th><th width=9%>Task Date</th><th>Update</th><th width=5%>Actions</th></tr>";
}

// draws table rows for current updates
function drawrestoftable($row)
{
 echo "<tr>";
 echo "<td>" .  $row['updatetime'] . "</td>";
 echo "<td>" .  $row['updatedate_fmt'] . "</td>";
 echo "<td id='small'><pre>" .  $row['updatetext'] . "</pre></td>";
 echo "<td id='tiny'><a href='editupdate.php?updateid=" .  $row['updateid'] . "'>Edit</a><br><a href='markasold.php?updateid=" .  $row['updateid'] . "&projectid=" . $row['projectid'] . "&isold=1'>Mark as Old</a><br><a href='deleteupdate.php?updateid=" . $row['updateid'] . "'>Delete</a></td>";
 echo "</tr>";
}

// draws table rows for previous updates
function drawrestoftableold($row)
{
 echo "<tr>";
 echo "<td>" .  $row['updatetime'] . "</td>";
 echo "<td>" .  $row['updatedate_fmt'] . "</td>";
 echo "<td><pre>" .  $row['updatetext'] . "</pre></td>";
 echo "<td id='tiny'><a href='editupdate.php?updateid=" .  $row['updateid'] . "'>Edit</a><br><a href='markasold.php?updateid=" .  $row['updateid'] . "&projectid=" . $row['projectid'] . "&isold=0'>Mark as New</a><br><a href='deleteupdate.php?updateid=" . $row['updateid'] . "'>Delete</a></td>";
 echo "</tr>";
}

include("mysql_conn.php");
include("settings.php");

// gets info about selected project
// $result=mysql_query("select * from projects where projectid='$foo'");
$result=mysql_query("select p.projectid,p.username,p.title,date_format(p.currduedate,'%m/%d/%Y') as currduedate,date_format(p.origduedate,'%m/%d/%Y') as origduedate,p.state,s.state_desc from projects p, states s where p.state=s.state and projectid='$foo'");
$data=mysql_fetch_array($result);

?>
<!-- html begins HERE -->
<html><head><title><? echo "$site_title"; ?>: Edit Project</title>
<link rel='stylesheet' type='text/css' href='style.css'>
</head><body>
<h3><a href="myprojects.php">My Projects</a>: <? print $data['title'] ?></h3>
<!-- nested tables for top -->
<!-- invisible header row -->
<table id=invisible width=85%><tr><td id=invisible width=50%>
<? if ($data['state']!='4') {echo "<h4>Edit Project Properties</h4>";} ?></td>
<td id=invisible width=50%>
<h4>Add New Updates</h4></td></tr>
<tr><td id=invisible>
<!-- top left -->
<? if ($data['state']!='4') {
echo "<form action='projedit-do.php' method='post'> <table> <tr><th>";
 echo "<input type='hidden' name='projectid' value='" . $data['projectid'] . "'>";
 echo "Owner: </th><td colspan='3'><input type='text' name='username' value='" . $data['username'] . "'></td></tr>";
 echo "<tr><th>Title: </th><td colspan='3'><input type='text' name='title' size='50' maxlength='50' value='" . $data['title'] . "'></td></tr>";
 echo "<tr><th>Original Due Date: </th><td><input type='text' name='origduedate' size='10' maxlength='10' disabled='disabled' value='" . $data['origduedate'] . "'>";
 echo "</td><th>Current Due Date: </th><td><input type='text' name='currduedate' size='10' maxlength='10' value='" . $data['currduedate'] . "'></td></tr>";
 echo "<tr><th colspan='2'>Project is:</th><td colspan='2'><input type='radio' name='state' value='1'"; 
 if ($data['state']=='1') echo "checked"; 
 echo "> Current <input type='radio' name='state' value='2'";
 if ($data['state']=='2') echo "checked";
 echo "> Non-Current <input type='radio' name='state' value='3'";
 if ($data['state']=='3') echo "checked";
 echo "> Old </td></tr>
</table>
<input type='submit' value='Apply'>
</form>";
}
?>
<!-- top right -->
</td><td id=invisible>
<form action="addupdate-do.php" method="post">
<table>
<th><input type="hidden" name="projectid" value="<? print $data['projectid'] ?>">
Type: </th><td><input type="radio" name="updatetype" value="T">Tasks Completed <input type="radio" name="updatetype" value="O">Obstacles  <input type="radio" name="updatetype" value="N">Next Steps</td><th>Date:</th><td><input type="text" name="updatedate" value="<? $now=time(); echo(date("m/d/Y", $now)) ?>" size="10"> </td>
<tr><td colspan="4"><textarea name="updatetext" cols="80" rows="4"></textarea></td></tr>
</table>
<input type="submit" value="Apply">
</form>
</td></tr>
<!-- displays project readiness if not non-project -->
<!-- <tr><td colspan="2"><? if ($data['state']!='4') echo "This project is <b>" . $data['state_desc'] . "</b>. Mark Project as: <a href='markready.php?projectid=$foo&state=1'>Current</a> -  <a href='markready.php?projectid=$foo&state=2'>Non-Current</a> -  <a href='markready.php?projectid=$foo&state=3'>Old</a>"; ?></td></tr></table> -->
</table>
<!-- start rest of page -->
<?php
//query for current tasks
$result2=mysql_query("select updateid,date_format(updatetime,'%m/%d/%Y') as updatetime,updatetext,date_format(updatedate,'%m/%d/%Y') as updatedate_fmt,isold,projectid from updates where projectid='$foo' and updatetype='T' and isold='0' order by updatedate desc $limit");

echo "<h3>New/Current Project Updates</h3>
<h4>Tasks Completed</h4>";
drawtableheader();

while($row=mysql_fetch_array($result2))
{
 drawrestoftable($row);
}
echo "</table>";
// current obstacles
$result3=mysql_query("select updateid,date_format(updatetime,'%m/%d/%Y') as updatetime,updatetext,date_format(updatedate,'%m/%d/%Y') as updatedate_fmt,isold,projectid from updates where projectid='$foo' and updatetype='O' and isold='0' order by updatedate desc $limit");

echo"<h4>Obstacles</h4>";
drawtableheader();

while($row=mysql_fetch_array($result3))
{
 drawrestoftable($row);
}
echo "</table>";
// current next steps
$result4=mysql_query("select updateid,date_format(updatetime,'%m/%d/%Y') as updatetime,updatetext,date_format(updatedate,'%m/%d/%Y') as updatedate_fmt,isold,projectid from updates where projectid='$foo' and updatetype='N' and isold='0' order by updatedate desc $limit");

echo"<h4>Next Steps</h4>";
drawtableheader();

while($row=mysql_fetch_array($result4))
{
 drawrestoftable($row);
}
echo "</table>";
// old tasks
$result5=mysql_query("select updateid,date_format(updatetime,'%m/%d/%Y') as updatetime,updatetext,date_format(updatedate,'%m/%d/%Y') as updatedate_fmt,isold,projectid from updates where projectid='$foo' and updatetype='T' and isold = '1' order by updatedate desc $limit");

echo "<p><a href='myprojects.php'>Return to My Projects</a><p><hr><p>
<h3>Older Project Updates</h3>
<h4>Tasks Completed</h4>";

drawtableheader();

while($row=mysql_fetch_array($result5))
{
 drawrestoftableold($row);
}
echo "</table>";
// old obstacles
$result6=mysql_query("select updateid,date_format(updatetime,'%m/%d/%Y') as updatetime,updatetext,date_format(updatedate,'%m/%d/%Y') as updatedate_fmt,isold,projectid from updates where projectid='$foo' and updatetype='O' and isold = '1' order by updatedate desc $limit");

echo"<h4>Obstacles</h4>";
drawtableheader();

while($row=mysql_fetch_array($result6))
{
 drawrestoftableold($row);
}
echo "</table>";
// old next steps
$result7=mysql_query("select updateid,date_format(updatetime,'%m/%d/%Y') as updatetime,updatetext,date_format(updatedate,'%m/%d/%Y') as updatedate_fmt,isold,projectid from updates where projectid='$foo' and updatetype='N' and isold = '1' order by updatedate desc $limit");

echo"<h4>Next Steps</h4>";
drawtableheader();

while($row=mysql_fetch_array($result7))
{
 drawrestoftableold($row);
}
echo "</table>";

?>

<p><a href="myprojects.php">Return to My Projects</a>

</body></html>


